package com.atjyb.auth.controller;

import com.atjyb.auth.service.SysRoleService;
import com.atjyb.common.result.Result;
import com.atjyb.model.system.SysRole;
import com.atjyb.vo.system.AssginRoleVo;
import com.atjyb.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService service;

    @ApiOperation("查询所有系统角色")
    @GetMapping("/getAllRoles")
    public Result getAllRoles() {
        return Result.ok(service.list());
    }

    /**
     * 条件分页查询
     * page 当前页
     * limit 分页数
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable long page,
                                @PathVariable long limit,
                                SysRoleQueryVo sysRoleQueryVo
    ) {
        //1.page对象，封装分页查询的条件
        Page<SysRole> pageParam = new Page(page, limit);
        //2.封装条件查询的条件
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)){
            //模糊查询
            lambdaQueryWrapper.like(SysRole::getRoleName,roleName);
        }

        return Result.ok(service.page(pageParam,lambdaQueryWrapper));
    }


    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加系统角色")
    @PostMapping("/save")
    public Result addSysRole(@RequestBody SysRole sysRole) {
        boolean saved = service.save(sysRole);
        if (saved){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据id获取系统角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysRole role = service.getById(id);
        return Result.ok(role);
    }


    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "根据id修改系统角色")
    @PutMapping("update")
    public Result updateById(@RequestBody SysRole role) {
        service.updateById(role);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id删除系统角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id列表删除系统角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        service.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = service.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        service.doAssign(assginRoleVo);
        return Result.ok();
    }
}
